package juc.thread;

import java.util.concurrent.*;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/03/07 08:25  2956,2297
 * 18758,19854
 */
public class CreateThread {
    public static void main(String[] args) throws InterruptedException {
        long start_time = System.currentTimeMillis();
        for (int i = 0; i < 5000000; i++) {
            System.out.print("");
        }
        System.out.println("\n" + Thread.currentThread().getName() + " " + (System.currentTimeMillis() - start_time));

//        A a = new A("A");
//        a.start();
//
//        Thread b = new Thread(() -> {
//            for (int i = 0; i < 1000000; i++) {
//                System.out.println(Thread.currentThread().getName() + "-" + i);
//            }
//        }, "B");
//        b.start();
//
//        Thread c = new Thread(new C(), "C");
//        c.start();
//
//        Runnable run = () -> {
//            for (int i = 0; i < 1000000; i++) {
//                System.out.println(Thread.currentThread().getName() + "-" + i);
//            }
//        };
//        Thread d = new Thread(run, "D");
//        d.start();
//
//        while (a.getState() != Thread.State.TERMINATED || a.getState() != b.getState() || b.getState() != c.getState() || c.getState() != d.getState()) {
//
//        }
//        new Thread(new C(), "A").start();
//        new Thread(new C(), "B").start();
//        new Thread(new C(), "C").start();
//        new Thread(new C(), "D").start();
//        new Thread(new C(), "E").start();

        ExecutorService executor = Executors.newSingleThreadExecutor();
//        executor.execute(new C("A"));
//        executor.execute(new C("B"));
//        executor.execute(new C("C"));
//        executor.execute(new C("D"));
//        executor.execute(new C("E"));
//        ExecutorService executor2 = Executors.newCachedThreadPool();
//        executor2.execute(new C("A2"));
//        executor2.execute(new C("B2"));
//        executor2.execute(new C("C2"));
//        executor2.execute(new C("D2"));
//        executor2.execute(new C("E2"));
//        ExecutorService executor3 = Executors.newWorkStealingPool();
//        executor3.execute(new C("A3"));
//        executor3.execute(new C("B3"));
//        executor3.execute(new C("C3"));
//        executor3.execute(new C("D3"));
//        executor3.execute(new C("E3"));
//        Executor executor1 = new ScheduledThreadPoolExecutor()
        Executor executor1 = new ThreadPoolExecutor(1,
                                                    5,
                                                    0,
                                                    TimeUnit.MILLISECONDS,
                                        new LinkedBlockingQueue<Runnable>());
        executor1.execute(new C("A"));
        executor1.execute(new C("B"));
        executor1.execute(new C("C"));
        executor1.execute(new C("D"));
        executor1.execute(new C("E"));
        System.out.println("over");
    }
}

class A extends Thread {
    public A(String name) {
        super(name);
    }

    @Override
    public void run() {
        long start_time = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            System.out.print(" ");
        }
        System.out.println("\n" + (System.currentTimeMillis() - start_time));
    }

}

class C implements Runnable {
    String name;
    public C(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        Thread.currentThread().setName(this.name);
        long start_time = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            System.out.print("");
//            System.out.println(Thread.currentThread().getName()+"  "+i);
        }
        System.out.println(Thread.currentThread().getName() + " " + (System.currentTimeMillis() - start_time));
    }
}